package com.example.mdoncall;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author Anthony Maniquiz & Laine Nicolas (Smart Telecom Inc. OJT) 
 * @version 1
 * 
 */

public class SearchPatientActivity extends Activity{
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_searchpatient);
		setOrientation();
		
		LinearLayout bLayout = (LinearLayout) findViewById(R.id.llayout);
		
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		
		for(Contact cn: contacts){
			final int id = cn.getID();
			TextView  tv = new TextView(this);
			tv.setTextSize(25);
			tv.setId(id);
			tv.setPadding(5, 5, 5, 5);
			tv.setBackgroundResource(R.drawable.back);
			tv.setText(cn.getFname() + " " + cn.getLname());
			tv.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
				goSearch(v, id);
				}
			});
	        bLayout.addView(tv);
		}
		
		db.close();
	}
	
	public void searchPatientInfo(View view){
		Intent intent = new Intent(this, ViewPatientActivity.class);
		
		EditText search = (EditText) findViewById(R.id.editText1);
		String strNumber = null;
		
		if( search.getText().toString().length() == 0 ){
			Toast.makeText(getApplicationContext(), "Patient's name required.", Toast.LENGTH_SHORT).show();
		} else {
			
			PersonDbHelper db = new PersonDbHelper(this);
			List<Contact> contacts = db.getAllContacts();
			
			for(Contact cn:contacts){
				if(cn.getFname().equalsIgnoreCase(search.getText().toString()) || cn.getLname().equalsIgnoreCase(search.getText().toString())){
					strNumber = cn.getPhoneNumber();
				}
			}
			
			db.close();

		intent.putExtra("search", strNumber);
		startActivity(intent);
		}
	}
	
	public void goSearch(View view, int id){
		Intent intent = new Intent(this, ViewPatientActivity.class);
		
		intent.putExtra("search", id);
		startActivity(intent);
	}
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }

}
